# Copyright 2017 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
# Copyright 2021 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
import sys

import numpy as np
import cv2


def parse_arguments(argv):
    parser = argparse.ArgumentParser()
    parser.add_argument('--om_vertex', type=str,
                        help='the path to load vertex model output file of om.', default="")
    parser.add_argument('--om_face', type=str,
                        help='the path to load face model output file of om.', default="")
    parser.add_argument('--gpu_vertex', type=str,
                        help='the path to load vertex model output file of gpu.', default="")
    parser.add_argument('--gpu_face', type=str,
                        help='the path to load face model output file of gpu.', default="")
    parser.add_argument('--pb_vertex', type=str,
                        help='the path to load vertex model output file of pb.', default="")
    parser.add_argument('--pb_face', type=str,
                        help='the path to load face model output file of pb.', default="")
    return parser.parse_args(argv)


def main(args):
    # Load file
    om_vertex = np.fromfile(args.om_vertex, dtype='float32').reshape([2, 400, 4])
    pb_vertex = np.fromfile(args.pb_vertex, dtype='float32').reshape([2, 400, 4])
    gpu_vertex = np.fromfile(args.gpu_vertex, dtype='float32').reshape([2, 400, 4])

    # Record the digital part of the images generated by pb,om and gpu
    pb_number = []
    om_number = []
    gpu_number = []
    for i in range(2):
        for j in range(400):
            for k in range(4):
                pixel_pb = pb_vertex[i][j][k]
                pixel_om = om_vertex[i][j][k]
                pixel_gpu = gpu_vertex[i][j][k]
                # eliminate the influence of background color whose pixel value close to zero
                if pixel_om > 1e-4 and pixel_pb > 1e-4 and pixel_gpu > 1e-4:
                    pb_number.append(pixel_pb)
                    om_number.append(pixel_om)
                    gpu_number.append(pixel_gpu)
    pb_number = np.array(pb_number)
    om_number = np.array(om_number)
    gpu_number = np.array(gpu_number)

    # The mean difference
    average_error_pb_om = np.abs(pb_number - om_number)
    average_error_gpu_om = np.abs(gpu_number - om_number)
    print("the average error of vertex model between pb and om:", np.mean(average_error_pb_om))
    print("the average error of vertex model between gpu and om:", np.mean(average_error_gpu_om))

    # The total difference
    total_error_pb_om = np.abs(pb_number - om_number).sum()
    total_error_gpu_om = np.abs(gpu_number - om_number).sum()
    print("the total error_of vertex model between pb and om:", total_error_pb_om)
    print("the total error of vertex model between gpu and om:", total_error_gpu_om)

    om_face = np.fromfile(args.om_face, dtype='float32').reshape([2, 2000])
    pb_face = np.fromfile(args.pb_face, dtype='float32').reshape([2, 2000])
    gpu_face = np.fromfile(args.gpu_face, dtype='float32').reshape([2, 2000])

    # Record the digital part of the images generated by pb,om and gpu
    pb_number = []
    om_number = []
    gpu_number = []
    for i in range(2):
        for j in range(2000):
            pixel_pb = pb_face[i][j]
            pixel_om = om_face[i][j]
            pixel_gpu = gpu_face[i][j]
            # eliminate the influence of background color whose pixel value close to zero
            if pixel_om > 1e-4 and pixel_pb > 1e-4 and pixel_gpu > 1e-4:
                pb_number.append(pixel_pb)
                om_number.append(pixel_om)
                gpu_number.append(pixel_gpu)
    pb_number = np.array(pb_number)
    om_number = np.array(om_number)
    gpu_number = np.array(gpu_number)

    # The mean difference
    average_error_pb_om = np.abs(pb_number - om_number)
    average_error_gpu_om = np.abs(gpu_number - om_number)
    print("the average error of face model between pb and om:", np.mean(average_error_pb_om))
    print("the average error of face model between gpu and om:", np.mean(average_error_gpu_om))

    # The total difference
    total_error_pb_om = np.abs(pb_number - om_number).sum()
    total_error_gpu_om = np.abs(gpu_number - om_number).sum()
    print("the total error_of face model between pb and om:", total_error_pb_om)
    print("the total error of face model between gpu and om:", total_error_gpu_om)


if __name__ == "__main__":
    main(parse_arguments(sys.argv[1:]))
